package logic

import (
	"context"
	"good/models/mysql"

	"good/goodrpc/goodrpc"
	"good/goodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodListLogic {
	return &GoodListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodListLogic) GoodList(in *goodrpc.GoodListRequest) (*goodrpc.GoodListResponse, error) {
	// todo: add your logic here and delete this line
	var goodData []mysql.Good
	offset := (in.Page - 1) * in.Size
	mysql.DB.Debug().Model(&mysql.Good{}).
		Joins("LEFT JOIN users ON users.id = goods.user_id").
		Joins("LEFT JOIN classes ON classes.id = goods.class_id").
		Where("goods.title like ?", in.Name).
		Offset(int(offset)).Limit(int(in.Size)).
		Find(&goodData)

	return &goodrpc.GoodListResponse{
		List: nil,
	}, nil
}
